﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UI.Admin.Content.ModelSlider
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
           // string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ContentAdmin", context.Request["ColumnID"]);
            //if (auth != "ACCEPT")
          //  {
          //      return;
          //  }

            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = ((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加滑动图片模型的内容
            if (dowhat == "add_slider")
            {
                string ColumnID = context.Request["ColumnID"];

                //发布者和最后编辑者
                string FirstEditor = UserName;
                string LastEditor = UserName;

                string Title = context.Request["Title"];
                string PictureAddress = context.Request["PictureAddress"];              
                string ArticleAddress = context.Request["ArticleAddress"];             
                string UpdateDate = DateTime.Now.ToString();
                string Remark = context.Request["Remark"];

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Create.InsertSlider(ColumnID, FirstEditor, LastEditor, Title, UpdateDate, PictureAddress, ArticleAddress, Remark) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加滑动图片模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 修改滑动模型的内容
            if (dowhat == "update_slider")
            {
                string ID = context.Request["ID"];
                string ColumnID = context.Request["ColumnID"];

                // 修改最后编辑者
                string LastEditor = UserName;

                string Title = context.Request["Title"];
                string PictureAddress = context.Request["PictureAddress"];
                string ArticleAddress = context.Request["ArticleAddress"];              
                string UpdateDate = DateTime.Now.ToString();
                string Remark = context.Request["Remark"];

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Update.UpdateSlider(ID, ColumnID, LastEditor, Title, UpdateDate, PictureAddress, ArticleAddress, Remark) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "修改滑动图片模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除滑动模型的内容
            if (dowhat == "delete_slider")
            {
                string ID = context.Request["ID"];

                string Title = BLL.Find.FindSlider(ID).Title;
                string Name = BLL.Find.FindOneColumn(context.Request["ColumnID"]).Name;

                if (BLL.Delete.DeleteSlider(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除滑动图片模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}